﻿using System.ComponentModel.Composition;
using System.ComponentModel.DataAnnotations;

namespace OperationPlugins.Validators.DataAnnotations
{
    [Export(typeof(IUsePluginValidatorsAttributeResolveOperationContextFactory))]
    [DefaultPriority]
    internal sealed class DefaultIUsePluginValidatorsAttributeResolveOperationContextFactory : IUsePluginValidatorsAttributeResolveOperationContextFactory
    {
        /// <summary>
        /// Resolves the operation context factory.
        /// </summary>
        /// <param name="value">The value to validate.</param>
        /// <param name="validationContext">Describes the context in which a data annotations validation check is performed.</param>
        /// <returns>The operation context factory.</returns>
        IOperationContextFactory IUsePluginValidatorsAttributeResolveOperationContextFactory.ResolveOperationContextFactory(object value, ValidationContext validationContext)
        {
            var e = new ResolveOperationContextFactoryEventArgs(value, validationContext);

            DataAnnotationsValidationResourceResolver.OnResolveOperationContextFactory(this, e);

            IOperationContextFactory factory = e.Resolved ?? OperationContextManager.GetOperationContextFactory();

            return factory;
        }
    }
}
